package prassein.rsm.web;

import java.io.InputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;

import prassein.rsm.common.utils.UploadConfigurationRead;
import prassein.rsm.entity.base.Resume;
import prassein.rsm.entity.base.ResumeFile;
import prassein.rsm.service.base.ResumeFileManager;
import prassein.rsm.service.base.ResumeManager;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@Results({ @Result(name = "success", location = "reloadReviewResume.jsp", type = "stream", params = {"inputName", "downloadFile"} ) })
public class DownLoadAction extends ActionSupport {
	private static final long serialVersionUID = 6329383258366253255L;
	private String fileName;
	private String fileRealName;
	private int order;
	private int resumeId = -1;
	@Autowired
	private ResumeManager resumeManager;
	
	@Autowired
	private ResumeFileManager resumeFileManager;
	public int getOrder() {
		return order;
	}

	public void setOrder(int order) {
		this.order = order;
	}

	public int getResumeId() {
		return resumeId;
	}

	public void setResumeId(int resumeId) {
		this.resumeId = resumeId;
	}

	public ResumeManager getResumeManager() {
		return resumeManager;
	}

	public void setResumeManager(ResumeManager resumeManager) {
		this.resumeManager = resumeManager;
	}

	public ResumeFileManager getResumeFileManager() {
		return resumeFileManager;
	}

	public void setResumeFileManager(ResumeFileManager resumeFileManager) {
		this.resumeFileManager = resumeFileManager;
	}

	public void setFileName()
	{
		// 得到请求下载的文件名
		String fname = ServletActionContext.getRequest().getParameter("name");
		String frealname = ServletActionContext.getRequest().getParameter("realname");
		try
		{
			/*
			 * 对fname参数进行UTF-8解码,注意:实际进行UTF-8解码时会使用本地编码，本机为GBK。
			 * 这里使用request.setCharacterEncoding解码无效.
			 * 只有解码了getDownloadFile()方法才能在下载目录下正确找到请求的文件
			 */
			fname = new String(fname.getBytes("ISO-8859-1"), "UTF-8");
			frealname= new String(frealname.getBytes("ISO-8859-1"), "UTF-8");
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		this.fileName = fname;
		this.fileRealName = frealname;
//		System.out.println(fileName);
//		System.out.println(fileRealName);
	}

	/*
	 * @getFileName 此方法对应的是struts.xml文件中的： <param
	 * name="contentDisposition">attachment;filename="${fileName}"</param>
	 * 这个属性设置的是下载工具下载文件时显示的文件名， 要想正确的显示中文文件名，我们需要对fileName再次编码
	 * 否则中文名文件将出现乱码，或无法下载的情况
	 */
	public String getFileName() throws UnsupportedEncodingException
	{
//		fileRealName = new String(fileRealName.getBytes(), "ISO-8859-1");

		return "";
	}

	/*
	 * @getDownloadFile 此方法对应的是struts.xml文件中的： <param
	 * name="inputName">downloadFile</param> 返回下载文件的流，可以参看struts2的源码
	 */
	public InputStream getDownloadFile() throws UnsupportedEncodingException
	{
		int resumeFileId = -1;
		Resume resume = resumeManager.getResume(resumeId);
		if (order == 1) {
			resumeFileId = resume.getResumeFile1();
		} else if (order == 2) {
			resumeFileId = resume.getResumeFile2();
		} else if (order == 3) {
			resumeFileId = resume.getResumeFile3();
		} else if (order == 4) {
			resumeFileId = resume.getResumeFile4();
		}
		ResumeFile fileEntity = resumeFileManager.get(resumeFileId);
		ActionContext ctx = ActionContext.getContext();       
		      
		HttpServletResponse response = (HttpServletResponse)ctx.get(ServletActionContext.HTTP_RESPONSE);
		response.setHeader("Content-disposition","attachment;filename="+ java.net.URLEncoder.encode(fileEntity.getFileName(), "UTF-8"));
		
		return ServletActionContext.getServletContext().getResourceAsStream("/"+UploadConfigurationRead.getInstance().getConfigItem("uploadFilePath").trim()+ "/" + fileEntity.getLocation());
	}

	@Override
	public String execute() throws Exception
	{
		return SUCCESS;
	}
}